package com.nexgo.payment.action.signature;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import com.nexgo.jbigkit.JbigKit;
import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.aop.annotation.RunOnMainThread;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.SignatureInfo;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.BmpUtils;
import com.nexgo.payment.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 电子签名P层实现
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年08月14日
 */
public class SignaturePresenter implements SignatureContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(SignaturePresenter.class.getSimpleName());

    private SignatureContract.View mView;
    private DataSource mRepository;
    private Bitmap mBitmap;
    private int tryTimes = 1;  //记录签名次数
    private byte[] mJbgData;

    public SignaturePresenter(@NonNull SignatureContract.View view, @NonNull DataSource repository, @NonNull TransData transData) {
        mView = checkNotNull(view);
        mRepository = checkNotNull(repository);
        view.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void clearSignature() {
        tryTimes++;
        if (tryTimes > getMaxSignature()) {
            postMainThread(() -> mView.showMaxSignatureTimes());
            return;
        }
        postMainThread(() -> mView.clearSignature(tryTimes));
    }

    @Override
    public void customerConfirmSignature(Bitmap bitmap) {
        mBitmap = null;
        mBitmap = bitmap;
        BmpUtils bmpUtils = new BmpUtils();
        if (mBitmap.getWidth() > 273) {
            Bitmap newBitmap = bmpUtils.zoomImg(mBitmap, 273, (273 * mBitmap.getHeight()) / mBitmap.getWidth());
            mBitmap.recycle();
            mBitmap = newBitmap;
        }
        //判断是否需要提示确认签名
        if ("1".equals(mRepository.getParamValue(PreferencesConstants.IS_SHOW_CONFIRM_SIGNATURE))) {
            postMainThread(() -> mView.showSignatureConfirm(mBitmap));
            return;
        }
        operatorConfirmSignature(true);
    }

    @Override
    public void operatorConfirmSignature(boolean confirm) {
        if (confirm) {
            mJbgData = new JbigKit(App.sContext).compress(mBitmap);
            if (mJbgData == null) {
                postMainThread(() -> mView.showSignatureErr());
                return;
            }
            if (mJbgData.length > 999) {
                postMainThread(() -> mView.showSignatureTooComplicated());
                return;
            }
            //签名完成
            postMainThread(() -> mView.showSignatureFinish(new SignatureInfo(null, mJbgData)));
        } else {
            clearSignature();
        }
    }

    @Override
    public void signatureTooComplicate() {
        tryTimes++;
        if (tryTimes <= getMaxSignature()) {
            postMainThread(() -> mView.showToast(App.sContext.getString(R.string.signature_too_complicated_and_sign_again)));
            postMainThread(() -> mView.clearSignature(tryTimes));
            return;
        }
        postMainThread(() -> mView.showToast(App.sContext.getString(R.string.signature_too_complicated)));
        postMainThread(() -> mView.showSignatureFail());
    }

    private int getMaxSignature() {
        return Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.MAX_SIGNATURE_TIMES), "3"));
    }

    @Override
    public int getSignatureTimeout() {
        return Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.SIGNATURE_TIME_OUT), "60"));
    }

    @RunOnMainThread
    private void postMainThread(Runnable runnable) {
        if (mView.isActive()) {
            runnable.run();
        }
    }
}
